<?php

/**
 * This is the model class for table "{{users}}".
 *
 * The followings are the available columns in table '{{users}}':
 * @property string $uid
 * @property string $user_unit
 * @property string $user_dpt
 * @property string $user_login
 * @property string $user_pass
 * @property string $user_tel
 * @property string $user_nicename
 * @property string $user_email
 * @property integer $user_rank
 * @property string $user_score
 * @property string $user_activation_key
 * @property integer $user_status
 * @property string $user_salt
 * @property string $created_at
 * @property string $updated_at
 * @property string $lastlogin_at
 */
class User extends CActiveRecord
{
	public $_verifypassword;
	public $_expassword;
	public $_userunitname;
	public $_userdptname;
	
	/**
	 * Returns the static model of the specified AR class.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{users}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('user_login, user_pass, _verifypassword, user_unit, user_dpt, user_tel, user_email', 'required', 'on'=>'insert'),
			array('user_rank, user_status', 'numerical', 'integerOnly'=>true),
			array('user_dpt, user_tel, user_score', 'length', 'max'=>20),
			array('user_login, user_activation_key', 'length', 'max'=>60),
			array('user_pass', 'length', 'max'=>64),
			array('user_nicename', 'length', 'max'=>50),
			array('user_email', 'length', 'max'=>100),
			array('user_salt', 'length', 'max'=>27),
			array('created_at, updated_at, lastlogin_at', 'safe'),
			array('_verifypassword', 'compare', 'compareAttribute'=>'user_pass', 'message' => Yii::t('user_msg', 'Retype Password is incorrect.')),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('uid, user_dpt, user_login, user_pass, user_tel, user_nicename, user_email, user_rank, user_score, user_activation_key, user_status, user_salt, created_at, updated_at, lastlogin_at', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'uid' => 'Uid',
			'user_login' => 'User Login',
			'user_pass' => 'User Pass',
			'_verifypassword' => 'Retype Pass',
			'user_unit' => 'User Unit',
			'user_dpt' => 'User Dpt',
			'user_tel' => 'User Tel',
			'user_nicename' => 'User Nicename',
			'user_email' => 'User Email',
			'user_rank' => 'User Rank',
			'user_score' => 'User Score',
			'user_activation_key' => 'User Activation Key',
			'user_status' => 'User Status',
			'user_salt' => 'User Salt',
			'created_at' => 'Created At',
			'updated_at' => 'Updated At',
			'lastlogin_at' => 'Lastlogin At',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('uid',$this->uid,true);
		$criteria->compare('user_dpt',$this->user_dpt,true);
		$criteria->compare('user_unit',$this->user_unit,true);
		$criteria->compare('user_login',$this->user_login,true);
		$criteria->compare('user_pass',$this->user_pass,true);
		$criteria->compare('user_tel',$this->user_tel,true);
		$criteria->compare('user_nicename',$this->user_nicename,true);
		$criteria->compare('user_email',$this->user_email,true);
		$criteria->compare('user_rank',$this->user_rank);
		$criteria->compare('user_score',$this->user_score,true);
		$criteria->compare('user_activation_key',$this->user_activation_key,true);
		$criteria->compare('user_status',$this->user_status);
		$criteria->compare('user_salt',$this->user_salt,true);
		$criteria->compare('created_at',$this->created_at,true);
		$criteria->compare('updated_at',$this->updated_at,true);
		$criteria->compare('lastlogin_at',$this->lastlogin_at,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	public function updateLastLogin($uid)
	{
		if (isset($uid) && $uid!='')
			User::model()->updateByPk($uid, array('lastlogin_at'=>date('Y-m-d H:i:s')));
	}

	/**
	 * 保存前初始化数据。
	 * @see CActiveRecord#beforeSave()
	 */
	protected function beforeSave()
	{
		if($this->isNewRecord)
		{
			$this->user_status = 1;
			$this->user_salt = $this->generateSalt();
			$this->user_pass = $this->hashPassword($this->user_pass, $this->user_salt);
			
			$this->created_at = date('Y-m-d H:i:s');
			$this->updated_at = date('Y-m-d H:i:s');
			
			$this->user_score = 100;//创建用户的初始积分为100；
		}
		else
		{
			$this->updated_at = date('Y-m-d H:i:s');
			if (isset($this->user_pass)&&!empty($this->user_pass))
			{
				$this->user_salt = $this->generateSalt();
				$this->user_pass = $this->hashPassword($this->user_pass, $this->user_salt);
			}
			else
			{
				$this->user_pass = $this->_expassword;
			}
		}
		return true;
	}
	
	/**
	 * Generates the password hash.
	 * @param string password
	 * @param string salt
	 * @return string hash
	 */
	static public function hashPassword($password,$salt)
	{
		return md5($password.$salt);
	}

	/**
	 * Generates a salt that can be used to generate a password hash.
	 * @return string the salt
	 */
	protected function generateSalt()
	{
		return uniqid('',true);
	}
	
	/**
	 * Checks if the given password is correct.
	 * @param string the password to be validated
	 * @return boolean whether the password is valid
	 */
	public function validatePassword($password)
	{
		return $this->hashPassword($password,$this->user_salt)===$this->user_pass;
	}
}